package com.drcoffee.drcoffeemall.controller;

import com.drcoffee.drcoffeemall.entry.Result;
import com.drcoffee.drcoffeemall.service.AttachmentService;
import com.drcoffee.drcoffeemall.entry.Attachment;
import com.drcoffee.drcoffeemall.util.FileUploadDownloadUtils;
import com.drcoffee.drcoffeemall.util.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/edit")
public class EditController {
    @Autowired
    FileUploadDownloadUtils fileUploadDownloadUtils;
    @Autowired
    AttachmentService attachmentService;

    @Autowired
    OSSUtil ossUtil;


    @PostMapping("/fileUpload")
    public Result fileUpload(@RequestParam("file") MultipartFile file,
                             @RequestParam("path") String path,
                             @RequestParam("name") String name) throws IOException {
        OSSUtil ossUtil = new OSSUtil();
        return ossUtil.upload(file, path, name);
        // return FileUploadDownloadUtils.uploadFile(file, path, name);
    }

//    @PostMapping("/fileUploadParts")
//    public Map<String, Object> fileUploadParts(@RequestParam("file") MultipartFile file,
//                             @RequestParam("path") String path,
//                             @RequestParam("name") String name) throws IOException {
//        return FileUploadDownloadUtils.uploadFile(file, path, name);
//    }

    @PostMapping("/fileUpload2")
    public Result fileUpload2(@RequestParam("file") MultipartFile file,
                              @RequestParam("path") String path,
                              @RequestParam("name") String name) throws IOException {
        return FileUploadDownloadUtils.uploadFile2(file, path, name);
    }


    @GetMapping("/downloadFile")
    // 在你的控制器方法上添加以下注解
    public Object downloadFile(@RequestParam("filePath") String filePath) throws IOException {
        String decodedFilePath = URLDecoder.decode(filePath, "UTF-8");
        System.out.println(filePath);
        return fileUploadDownloadUtils.downloadFile(decodedFilePath.replace('/', '\\'));
    }

    @PostMapping("/delfile")
    public Object Delefile(@RequestParam("url") String url) {
        return FileUploadDownloadUtils.delFile(url);
    }

    //配件商场编辑价格同步至OCM
    @PostMapping("/editInfo")
    public Map<String, Object> editInfo(@RequestBody Attachment attachment) {
        return updateOCM(attachment);
    }

    public Map<String, Object> updateOCM(Attachment attachment) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            attachmentService.editInfo(attachment);
            resultMap = attachmentService.updateOCM(attachment);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("reason", e.toString());
        }
        return resultMap;
    }

    @PostMapping("/partImageProcessing")
    public Map<String, Object> partImageProcessing(@RequestParam("file") MultipartFile file) throws IOException {
        return ossUtil.uploadImgPartTmp(file);
    }


}
